/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.musictalk.model.connect;

import com.musictalk.message.model.*;
import com.musictalk.model.business.ManageChat;
import com.musictalk.model.business.ManageChatRoom;
import com.musictalk.model.business.ManageClientConnThread;
import com.musictalk.model.business.ManageFriendList;
import com.musictalk.view.ChatFrame;
import com.musictalk.view.ChatRoom;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author AIDEN
 */
public class ClientConnThread extends Thread{

    private Socket tSocket;
    private User logUser;

    public ClientConnThread(Socket tSocket,User user) {
        this.logUser = user;
        this.tSocket = tSocket;
    }

    @Override
    public void run(){
        while (true) {
            try {
                ObjectInputStream ois = new ObjectInputStream(tSocket.getInputStream());
                Object temp = ois.readObject();
                Message m = (Message)temp;
                if (m.getMsgType()==Message.MSG_NORMAL) {
                    NormalMessage nMessage = (NormalMessage)temp;
                    ChatFrame chat = ManageChat.getChat(nMessage.getReceiver().getUID()+":"+nMessage.getSender().getUID());
                    if (chat!=null) {
                        chat.updateOutputText(nMessage);
                    }else{
                        ChatFrame nchat = new ChatFrame(nMessage.getReceiver(),nMessage.getSender());
                        ManageChat.addChat(nMessage.getReceiver().getUID()+":"+nMessage.getSender().getUID(),nchat );
                        nchat.setVisible(true);
                        nchat.updateOutputText(nMessage);
                    }
                }else if(m.getMsgType()==Message.MSG_CHAT_ROOM){
                    RoomChat rMsg = (RoomChat)temp;
//                    System.out.println(rMsg.getSender()+":"+rMsg.getMsg());
//                    ManageChatRoom.getChatRoom(logUser.getUID()).updateMsg(rMsg);
                    ChatRoom cr = ManageChatRoom.getChatRoom(logUser.getUID());
                    if (cr!=null) {
                        cr.updateMsg(rMsg);
                    }else{
                        ChatRoom ncr = new ChatRoom(logUser);
                        ManageChatRoom.addChatRoom(logUser.getUID(), ncr);
                        ncr.setVisible(true);
                        ncr.updateMsg(rMsg);
                    }
                }else if (m.getMsgType() == Message.MSG_RETURN_ONLINE_FRIEND) {
                    OnlineFriend onlineFriend = (OnlineFriend)temp;
                    ManageFriendList.getFriendList(onlineFriend.getReceiverID()).updateOnlineFriend(onlineFriend);
                }else if (m.getMsgType() == Message.MSG_TEST) {
                    TestMessage test = (TestMessage) temp;
                    System.out.println(test.getMsg());
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ClientConnThread.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                ManageClientConnThread.removeThread(logUser.getUID());
//                Logger.getLogger(ClientConnThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public Socket gettSocket() {
        return tSocket;
    }
    
}
